package com.codingvip.link;

import javax.xml.soap.Node;

/**
 * @Author Tony.Han
 * @Created at 2020/4/3 11:45 上午
 * Desc:
 */
public class ChangeList {

    public static void main(String[] args) {
        ListNode node = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        ListNode node5 = new ListNode(5);
        node.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next/**/ = null;
        ListNode h = node;
        while (h != null) {
            System.out.println(h.val);
            h = h.next;
        }

        ListNode changeNode = change(node);
        h = changeNode;
        while (h != null) {
            System.out.println(h.val);
            h = h.next;
        }
    }

    public static ListNode change(ListNode root) {
        ListNode h, p, q, pre = null;
        if (root == null) {
            return root;
        }
        h = root;
        while (root != null && root.next != null) {
            p = root.next;
            q = p.next;
            root.next = q;
            p.next = root;
            if (pre != null) {
                pre.next = p;
            }else {
                h = p;
            }
            pre = root;
            root = q;


        }
        return h;
    }

}
